package vue;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import modele.utilisateurs.Administrateur;
import modele.utilisateurs.Personne;
import modele.utilisateurs.PersonneFactory;

public class AjouterPersonnePanel extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 3L;
	private JTextField textFieldNom;
	private JTextField textFieldPrenom;
	private JTextField textFieldMdp;

	/**
	 * Create the panel.
	 */
	public AjouterPersonnePanel(final Dialog dialog, final Fenetre fenetre) {
		setLayout(null);
		setSize(300, 198);
		JLabel lblNom = new JLabel("Nom :");
		lblNom.setBounds(25, 14, 83, 14);
		add(lblNom);
		
		JLabel lblPrenom = new JLabel("Pr\u00E9nom :");
		lblPrenom.setBounds(25, 45, 83, 14);
		add(lblPrenom);
		
		JLabel lblMotDePasse = new JLabel("Mot de passe :");
		lblMotDePasse.setBounds(25, 76, 83, 14);
		add(lblMotDePasse);
		
		JLabel lblType = new JLabel("Type :");
		lblType.setBounds(25, 107, 83, 14);
		add(lblType);
		
		textFieldNom = new JTextField();
		textFieldNom.setBounds(118, 11, 156, 20);
		add(textFieldNom);
		textFieldNom.setColumns(10);
		
		textFieldPrenom = new JTextField();
		textFieldPrenom.setColumns(10);
		textFieldPrenom.setBounds(118, 42, 156, 20);
		add(textFieldPrenom);
		
		textFieldMdp = new JTextField();
		textFieldMdp.setColumns(10);
		textFieldMdp.setBounds(118, 73, 156, 20);
		add(textFieldMdp);
		
		final JComboBox comboBoxType = new JComboBox();
		comboBoxType.setBounds(118, 104, 156, 20);
		comboBoxType.addItem("Abonne");
		comboBoxType.addItem("Administrateur");
		add(comboBoxType);
		
		JButton btnAjouter = new JButton("Ajouter");
		btnAjouter.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String nom = textFieldNom.getText();
				String prenom = textFieldPrenom.getText();
				String mdp = textFieldMdp.getText();
				String type = comboBoxType.getSelectedItem().toString();
				
				if(!nom.equals("") && !prenom.equals("") && !mdp.equals(""))
				{
					try {
						Personne personne = PersonneFactory.createPersonne(nom, prenom, mdp, type);
						boolean rafraichir =  Administrateur.ajouterPersonne(personne, type);
						fenetre.adminPanel.rafraichirTablePersonnes(rafraichir);
						dialog.setVisible(false);
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		});
		btnAjouter.setBounds(25, 151, 89, 23);
		add(btnAjouter);
		
		JButton btnAnnuler = new JButton("Annuler");
		btnAnnuler.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dialog.setVisible(false);
			}
		});
		btnAnnuler.setBounds(185, 151, 89, 23);
		add(btnAnnuler);
	}
}
